package com.xnew.utils;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author anonymous
 * @Date 2020/10/14
 */
public class MybatisParam extends HashMap<String,Object> {

    private Integer pageNum;

    private Integer pageSize;

    public static MybatisParam generator(){
        return generator(null);
    }

    public static MybatisParam generator(DataInput input){
        MybatisParam mybatisParam = new MybatisParam ();
        if(input != null){
            mybatisParam.setPageNum(input.getPageNum());
            mybatisParam.setPageSize(input.getPageSize());
            if(StringUtils.isNotEmpty(input.getSearchText())){
                mybatisParam.put("searchText","%"+input.getSearchText()+"%");
            }
            if(StringUtils.isNotEmpty(input.getSortField())){
                mybatisParam.put("sortField",input.getSortField());
            }
            if(StringUtils.isNotEmpty(input.getSortOrder())){
                mybatisParam.put("sortOrder",input.getSortOrder());
            }
        }
        return mybatisParam;
    }

    public static DataOutput getOutput(List list){
        PageInfo pageInfo = new PageInfo(list);
        DataOutput dataTablesOutput = DataOutput.success();
        dataTablesOutput.setData(pageInfo.getList());
        dataTablesOutput.setRecordsTotal(pageInfo.getTotal());
        dataTablesOutput.setRecordsFiltered(pageInfo.getTotal());
        return dataTablesOutput;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public Object put(String key, Object value) {
        if(value instanceof String){
            if(value != null && StringUtils.isNotEmpty((String)value)){
                return super.put(key,value);
            }
        }else{
            return super.put(key, value);
        }
        return null;
    }

    public static DataOutput getOutputData(List list, int pageNum, int pageSize){
        Map<String, List> map = new HashMap<String, List>();
        String redisKey = "DATA:"+IdUtils.getIncreaseIdByNanoTime();
        RedisUtil.set(redisKey, JSONObject.toJSONString(list));//缓存数据
        //处理分页
        DataOutput output = DataOutput.success();
        List returnList = new ArrayList();
        int size = list.size();
        if(pageNum*pageSize <= size){
            size = pageNum*pageSize;
        }
        for(int i = (pageNum - 1)*pageSize; i < size; i++){
            returnList.add(list.get(i));
        }
        output.setData(returnList);
        output.setRecordsTotal(list.size());
        output.setDataNo(redisKey);
        return output;
    }
}
